package org.openrisk.domain;

import static javax.persistence.GenerationType.AUTO;
import static javax.persistence.TemporalType.DATE;
import static javax.persistence.TemporalType.TIMESTAMP;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToOne;
import javax.persistence.Version;

import org.openrisk.listeners.BaseListener;

@MappedSuperclass
@EntityListeners({BaseListener.class})
public abstract class Base implements Temporal, Serializable {

	private static final long serialVersionUID = -7741212722258033975L;
	
	@Id
	@GeneratedValue (strategy=AUTO)
	private Long id;

	// Object persistence lifecycle
	
	@Version
	private Integer version;
	
    @OneToOne(optional=true)
	private User createdBy = User.SYSTEM;
    
    @javax.persistence.Temporal(TIMESTAMP)
    private Date creationDate;
	
    @OneToOne(optional=true)
	private User changedBy;

    @javax.persistence.Temporal(TIMESTAMP)
    private Date changeDate;
    
    // temporal for all Objects

    @javax.persistence.Temporal(DATE)
    private Date startDate;
    @javax.persistence.Temporal(DATE)
    private Date finishDate;
    
	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @return the version
	 */
	public Integer getVersion() {
		return version;
	}

	/**
	 * @param version the version to set
	 */
	public void setVersion(Integer version) {
		this.version = version;
	}

	/**
	 * @return the createdBy
	 */
	public User getCreatedBy() {
		return createdBy;
	}

	/**
	 * @param createdBy the createdBy to set
	 */
	public void setCreatedBy(User createdBy) {
		this.createdBy = createdBy;
	}

	/**
	 * @return the changedBy
	 */
	public User getChangedBy() {
		return changedBy;
	}

	/**
	 * @param changedBy the changedBy to set
	 */
	public void setChangedBy(User changedBy) {
		this.changedBy = changedBy;
	}

	/**
	 * @return the creationDate
	 */
	public Date getCreationDate() {
		return creationDate;
	}

	/**
	 * @param creationDate the creationDate to set
	 */
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	/**
	 * @return the changeDate
	 */
	public Date getChangeDate() {
		return changeDate;
	}

	/**
	 * @param changeDate the changeDate to set
	 */
	public void setChangeDate(Date changeDate) {
		this.changeDate = changeDate;
	}

	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the finishDate
	 */
	public Date getFinishDate() {
		return finishDate;
	}

	/**
	 * @param finishDate the finishDate to set
	 */
	public void setFinishDate(Date finishDate) {
		this.finishDate = finishDate;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Base [id=" + id + ", version=" + version + ", createdBy="
				+ createdBy + ", creationDate=" + creationDate + ", changedBy="
				+ changedBy + ", changeDate=" + changeDate + ", startDate="
				+ startDate + ", finishDate=" + finishDate + "]";
	}

}
